<template>
  <div class="login-container">
    <!-- 动态粒子背景 -->
    <div id="particles-js"></div>
    <div id="water-wave"></div>

    <!-- 登录表单 -->
    <form class="login-form" @submit.prevent="handleSubmit">
      <h2>用户登录</h2>
      <div class="form-group">
        <label>用户名</label>
        <input type="text" v-model="formData.username" placeholder="请输入用户名" required />
        <span class="error-msg" v-if="errors.username">{{ errors.username }}</span>
      </div>

      <div class="form-group">
        <label>密码</label>
        <input type="password" v-model="formData.password" placeholder="请输入密码" required />
        <span class="error-msg" v-if="errors.password">{{ errors.password }}</span>
      </div>

      <div class="remember-me">
        <input type="checkbox" v-model="formData.remember" />
        <label>记住我</label>
      </div>

      <button type="submit" class="login-btn" :disabled="isSubmitting">
        {{ isSubmitting ? '登录中...' : '立即登录' }}
      </button>

      <div class="additional-links">
        <a href="#forgot">忘记密码？</a>
        <router-link to="/register">注册账号</router-link>
      </div>
    </form>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import router from '@/router'
import { reactive, ref } from 'vue'
import { login } from '@/api/user' // 假设登录API在此路径

const formData = reactive({
  username: 'admin',
  password: '1234',
  remember: false,
})

const errors = reactive({
  username: '',
  password: '',
})

const isSubmitting = ref(false)

const validateForm = () => {
  let isValid = true

  if (!formData.username.trim()) {
    errors.username = '用户名不能为空'
    isValid = false
  } else {
    errors.username = ''
  }

  if (!formData.password) {
    errors.password = '密码不能为空'
    isValid = false
  } else if (formData.password.length < 4) {
    errors.password = '密码至少需要4位'
    isValid = false
  } else {
    errors.password = ''
  }

  return isValid
}

const handleSubmit = async () => {
  if (!validateForm()) return // 验证表单

  try {
    const response = await login({
      username: formData.username.trim(),
      password: formData.password,
    })

    // 检查响应状态码
    if (response.code !== 200) {
      ElMessage.error(response.message || '登录失败，请检查用户名或密码')
      return
    }

    ElMessage.success('登录成功')

    // 确保从 response.data 中获取用户信息
    const user = response.data?.user
    const token = response.data?.token

    if (!user || !token) {
      console.error('后端返回的数据不完整:', response)
      ElMessage.error('登录失败，用户信息不完整')
      return
    }

    // 将用户信息存储到 localStorage
    localStorage.setItem('user', JSON.stringify(user))
    localStorage.setItem('token', token)

    console.log('用户信息已存储:', localStorage.getItem('user'))

    router.push('/Visualization') // 跳转到可视化大屏
  } catch (error) {
    console.error('登录失败:', error)
    const errorMessage = error.response?.data?.message || '登录失败，请稍后重试'
    ElMessage.error(errorMessage)
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  width: 100%;
  height: 100vh;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden; /* 防止水波溢出 */
}

#particles-js {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: -1; /* 确保粒子背景在后面 */
}

#water-wave {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 300%;
  height: 300%;
  background: radial-gradient(circle, rgba(79, 172, 254, 0.6) 25%, transparent 80%);
  animation: wave 6s infinite ease-in-out;
  transform: translate(-50%, -50%);
  z-index: -1; /* 确保水波背景在后面 */
}

#water-wave::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 300%;
  height: 300%;
  background: radial-gradient(circle, rgba(0, 242, 254, 0.4) 25%, transparent 80%);
  animation: wave 8s infinite ease-in-out reverse;
  transform: translate(-50%, -50%);
}

#water-wave::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 300%;
  height: 300%;
  background: radial-gradient(circle, rgba(0, 122, 254, 0.3) 25%, transparent 80%);
  animation: wave 10s infinite ease-in-out;
  transform: translate(-50%, -50%);
}

.login-form {
  background: rgba(255, 255, 255, 0.8); /* 半透明白色背景 */
  padding: 2.5rem 3rem;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1); /* 调整阴影透明度 */
  width: 420px;
  animation: fadeIn 0.5s ease-in-out;
}

h2 {
  text-align: center;
  color: #333;
  margin-bottom: 2rem;
  font-size: 1.8rem;
  font-weight: bold;
  position: relative;
}

h2::after {
  content: '';
  display: block;
  width: 50px;
  height: 4px;
  background: #4facfe; /* 水利主题颜色 */
  margin: 0.5rem auto 0;
  border-radius: 2px;
}

.form-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  color: #555;
  font-weight: 500;
}

input[type='text'],
input[type='password'] {
  width: 100%;
  padding: 0.9rem;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 1rem;
  transition:
    border-color 0.3s,
    box-shadow 0.3s;
}

input:focus {
  outline: none;
  border-color: #4facfe; /* 聚焦时的水利主题颜色 */
  box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.2);
}

.error-msg {
  color: #f56c6c;
  font-size: 0.875rem;
  margin-top: 0.25rem;
  display: block;
}

.remember-me {
  display: flex;
  align-items: center;
  margin-bottom: 1rem;
}

.remember-me input {
  margin-right: 0.5rem;
}

.login-btn {
  width: 100%;
  padding: 0.9rem;
  background: linear-gradient(135deg, #4facfe, #00f2fe); /* 水利主题按钮渐变 */
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: bold;
  transition:
    background 0.3s,
    transform 0.2s;
}

.login-btn:hover:not(:disabled) {
  background: linear-gradient(135deg, #00f2fe, #4facfe); /* 悬停时渐变反转 */
  transform: translateY(-2px);
}

.login-btn:disabled {
  background: #a0cfff;
  cursor: not-allowed;
}

.additional-links {
  margin-top: 1.5rem;
  display: flex;
  justify-content: space-between;
}

.additional-links a {
  color: #4facfe; /* 水利主题链接颜色 */
  text-decoration: none;
  font-size: 0.875rem;
}

.additional-links a:hover {
  text-decoration: underline;
}

@keyframes wave {
  0% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
  50% {
    transform: translate(-50%, -50%) scale(1.2);
    opacity: 0.6;
  }
  100% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
